package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.rfid.project.exception.CannotUpdateException;
import com.mysema.query.BooleanBuilder;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class PositionAuthorityService {

	@Autowired
	private PositionAuthorityDao positionAuthorityDao;

	private QPositionAuthority $ = QPositionAuthority.positionAuthority;

	@Autowired
	public void setPositionAuthorityDao(
			PositionAuthorityDao positionAuthorityDao) {
		this.positionAuthorityDao = positionAuthorityDao;
	}

	public List<PositionAuthority> findAll() {
		return ListUtil.toList(positionAuthorityDao.findAll());
	}

	public List<PositionAuthority> findAll(Long positionId, Long authorityId,
			String orderByAttribute) {
		BooleanBuilder condition = new BooleanBuilder();

		if (IdUtil.isNotEmpty(positionId)) {
			condition.and($.position.id.eq(positionId));
		}

		if (IdUtil.isNotEmpty(authorityId)) {
			condition.and($.authority.id.eq(authorityId));
		}

		return ListUtil.toList(positionAuthorityDao.findAll(condition));
	}

	public Page<PositionAuthority> findAll(Long positionId, Long authorityId,
			int pageNumber, int pageSize, String sortAttribute,
			String orderDirection) {
		BooleanBuilder condition = new BooleanBuilder();

		if (IdUtil.isNotEmpty(positionId)) {
			condition.and($.position.id.eq(positionId));
		}
		if (IdUtil.isNotEmpty(authorityId)) {
			condition.and($.authority.id.eq(authorityId));
		}

		Sort sortObject = null;
		if (StringUtil.isEmpty(sortAttribute)) {
			sortObject = new Sort(Direction.DESC, "id");
		} else {
			sortObject = new Sort(Direction.fromStringOrNull(orderDirection),
					sortAttribute);
		}

		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize,
				sortObject);
		return positionAuthorityDao.findAll(condition, pageRequest);
	}

	public PositionAuthority getPositionAuthority(Long id) {
		return positionAuthorityDao.getOne(id);
	}

	public void deletePositionAuthority(Long id) {
		positionAuthorityDao.delete(id);
	}

	public void savePositionAuthority(PositionAuthority positionAuthority) throws CannotUpdateException {
		positionAuthorityDao.save(positionAuthority);
	}
	
	public List<Authority> findAuthorityByPosition(Position position){
		BooleanBuilder condition = new BooleanBuilder();
		condition.and($.position.id.eq(position.getId()));
		List<Authority> authorities = new ArrayList<Authority>();
		List<PositionAuthority> positionAuthoritys = ListUtil.toList(this.positionAuthorityDao.findAll(condition));
		for (PositionAuthority positionAuthority : positionAuthoritys) {
			authorities.add(positionAuthority.getAuthority());
		}
		return authorities;
	}

}
